<template>
  <div style="margin: 20px; margin-top: 60px;">
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="auto"
    >
      <el-form-item :label="t('form.startwellDepth')" prop="startwelldepth">
        <el-input
          v-model="welldepth.startwelldepth"
          :placeholder="t('form.inputStartWell')"
          clearable
          @keyup.enter="handleQuery"
          @input="updateWellDepth"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item :label="t('form.endwelldepth')" prop="endwelldepth">
        <el-input
          v-model="welldepth.endwelldepth"
          :placeholder="t('form.inputEndWell')"
          clearable
          @keyup.enter="handleQuery"
          @input="updateWellDepth"
          class="!w-240px"
        />
      </el-form-item>


      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> {{ t('action.search') }}</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> {{ t('action.reset') }}</el-button>
<!--        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['base:WITS8:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['base:WITS8:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>-->
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      v-loading="loading"
      :data="visibleItems"
      :stripe="true"
      :show-overflow-tooltip="true"
      style="width: 100%"
      @mouseenter="stopScroll"
      @mouseleave="startScroll"
    >
      <el-table-column :label="t('searchbar.WellCode')" align="center" prop="wellCode" />
      <el-table-column :label="t('searchbar.deviceCode')" align="center" prop="deviceCode" />
      <el-table-column :label="t('searchbar.RecordIdentifier')" align="center" prop="recId" />
      <el-table-column :label="t('searchbar.SequenceIdentifier')" align="center" prop="seqId" />

      <el-table-column :label="t('searchbar.DateTimeDataCenter')" align="center" width="180px">
        <template #default="scope"> {{ scope.row.dataDate }} {{ scope.row.dataTime }} </template>
      </el-table-column>

      <el-table-column :label="t('searchbar.dateTime')" align="center" width="180px">
        <template #default="scope"> {{ scope.row.date }} {{ scope.row.time }} </template>
      </el-table-column>

      <el-table-column :label="t('searchbar.ActivityCode')" align="center" prop="actCode" />
      <el-table-column :label="t('searchbar.MeasurementDepth')" align="center" prop="measDepth" />
      <el-table-column :label="t('searchbar.VerticalDepth')" align="center" prop="vertDepth" />
      <el-table-column :label="t('searchbar.BitDepth')" align="center" prop="bitDepth" />
      <el-table-column :label="t('searchbar.BitVerticalDepth')" align="center" prop="bitVDepth" />
      <el-table-column :label="t('searchbar.PassNumber')" align="center" prop="passNum" />
      <el-table-column :label="t('searchbar.DeepResistivityDepth')" align="center" prop="r1Depth" />
      <el-table-column :label="t('searchbar.DeepResistivityVerticalDepth')" align="center" prop="r1VDepth" />
      <el-table-column :label="t('searchbar.DeepResistivityReading')" align="center" prop="r1Reading" />
      <el-table-column :label="t('searchbar.DeepResistivityCorrectedReading')" align="center" prop="r1CReading" />
      <el-table-column :label="t('searchbar.ShallowResistivityDepth')" align="center" prop="r2Depth" />
      <el-table-column :label="t('searchbar.ShallowResistivityVerticalDepth')" align="center" prop="r2VDepth" />
      <el-table-column :label="t('searchbar.ShallowResistivityReading')" align="center" prop="r2Reading" />
      <el-table-column :label="t('searchbar.ShallowResistivityCorrectedReading')" align="center" prop="r2CReading" />
      <el-table-column :label="t('searchbar.GammaRay1Depth')" align="center" prop="g1Depth" />
      <el-table-column :label="t('searchbar.GammaRay1VerticalDepth')" align="center" prop="g1VDepth" />
      <el-table-column :label="t('searchbar.GammaRay1Reading')" align="center" prop="g1Reading" />
      <el-table-column :label="t('searchbar.GammaRay1CorrectedReading')" align="center" prop="g1CReading" />
      <el-table-column :label="t('searchbar.remark')" align="center" prop="remark" />
      <el-table-column
        :label="t('searchbar.createTime')"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
<!--      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['base:WITS8:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['base:WITS8:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>-->
    </el-table>

  </ContentWrap>
  </div>
  <!-- 表单弹窗：添加/修改 -->
  <WITS8Form ref="formRef" @success="getList" />

</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { WITS8Api, WITS8VO } from '@/api/search/loggdata/wits8'
import WITS8Form from './WITS8Form.vue'
import {ElFormItem} from "element-plus";

/** WITS8生成的表 列表 */
defineOptions({ name: 'WITS8' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<WITS8VO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  /*pageNo: 1,
  pageSize: 10,*/
  measDepth: [],
})

const welldepth = reactive({
  startwelldepth: undefined,
  endwelldepth: undefined
})
const visibleItems = ref([]) // 初始显示5条数据
let scrollInterval = null
let currentIndex = 0 // 当前显示数据的起始索引
const updateWellDepth = () => {
  queryParams.measDepth = [welldepth.startwelldepth, welldepth.endwelldepth]
}

const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中




/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await WITS8Api.getNewList(queryParams)
    list.value = data.data
    total.value = data.total
    visibleItems.value = list.value.slice(0, 15)
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  welldepth.startwelldepth = undefined
  welldepth.endwelldepth = undefined
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await WITS8Api.deleteWITS8(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await WITS8Api.exportWITS8(queryParams)
    download.excel(data, 'WITS8生成的表.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
  startScroll()
})


const scrollTable = async() => {
  // const data = await WITS8Api.getNewOne({})
  // list.value.push(data)

  if (list.value.length - currentIndex <= 15) {
    // 如果数据显示到最后5条数据时停止滚动
    stopScroll()
    addData()
    return
  }
  // 滚动逻辑：将当前显示的数据下移一行
  visibleItems.value = list.value.slice(currentIndex + 1, currentIndex + 16)
  currentIndex++
}
const startScroll = () => {
  scrollInterval = setInterval(scrollTable, 2000)
}
const stopScroll = () => {
  clearInterval(scrollInterval)
}
/*const handleMouseWheel = () => {
  visibleItems.value = list.value
}*/
onUnmounted(() => {
  stopScroll()
})
// // 添加新数据的方法
const addData = async()=>{
  const data = await WITS8Api.getNewOne({})
  list.value.push(data)
  if(visibleItems.value.length<15){
    visibleItems.value.push(data)
    startScroll()
  }
  if (list.value.length > 15) {
    // 如果数据超过5条，继续滚动
    startScroll()
  }
}


</script>
